package com.octopus.string;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.codec.binary.Base64;

public class StringUtils {
	public static String concat(String... strings) {
		StringBuffer result = new StringBuffer();
		for (String tmp : strings) {
			result.append(tmp);
		}
		return result.toString();
	}

	public static StringBuffer concatInBuffer(StringBuffer buf,
			String... strings) {
		if (buf == null) {
			buf = new StringBuffer();
		}
		for (String tmp : strings) {
			buf.append(tmp);
		}
		return buf;
	}

	public static String base64en(byte value[])
			throws UnsupportedEncodingException {
		Base64 b64 = new Base64();
		byte[] result = b64.encode(value);
		return new String(result, "UTF-8");
	}

	public static byte[] base64de(String string)
			throws UnsupportedEncodingException {
		Base64 b64 = new Base64();
		byte[] result = b64.decode(string.getBytes("UTF-8"));
		return result;
	}

	public static String printHashMapToString(Map<Object, Object> hm) {
		StringBuffer sb = new StringBuffer();
		if (hm == null) {
			return "NULL";
		}
		Set<Map.Entry<Object, Object>> set = hm.entrySet();
		Iterator<Map.Entry<Object, Object>> iterator = set.iterator();
		while (iterator.hasNext()) {
			Map.Entry<Object, Object> entry = iterator.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			sb.append(StringUtils.concat("key[",
					(key == null ? null : key.toString()), "]value[",
					(value == null ? null : value.toString()), "]\n"));
		}
		return sb.toString();
	}

	public static boolean stringEquals(String string1, String string2) {
		if(string1 == null && string2 == null){
			return true;
		}
		if (string1 == null || string2 == null) {
			return false;
		}
		return string1.equals(string2);
	}

	public static boolean stringEqualsIgnoreCase(String stirng1, String string2) {
		if (stirng1 == null || string2 == null) {
			return false;
		}
		return stirng1.equalsIgnoreCase(string2);
	}

	public static String exceptionToString(Exception e) {
		String result = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(baos);
		e.printStackTrace(ps);
		try {
			result = baos.toString();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (Exception eee) {
			}
			try {
				if (baos != null) {
					baos.close();
				}
			} catch (Exception eee) {
			}
		}
		return result;
	}
	
	public static boolean isNullString(String string){
		if(string == null || string.trim().isEmpty()){
			return true;
		}
		return false;
	}
	
	public static class StringContainer{
		private String string;
		private String afterTrimedString;

		public StringContainer(String string){
			this.string = string;
		}
		
		public String getString() {
			return string;
		}

		public void setString(String string) {
			this.string = string;
		}

		public String getAfterTrimedString() {
			return afterTrimedString;
		}

		public void setAfterTrimedString(String afterTrimedString) {
			this.afterTrimedString = afterTrimedString;
		}
		
		
	}
	
	public static boolean isNullString(StringContainer stringContainer){
		if(stringContainer == null){
			return true;
		}
		if(stringContainer.getString() == null){
			return true;
		}
		stringContainer.setAfterTrimedString(stringContainer.getString().trim());
		return stringContainer.getAfterTrimedString().isEmpty();
	}
	
	public static String inputStreamToRightString(InputStream is, String charset) throws IOException{
		if(is == null){
			return null;
		}
		if(isNullString(charset)){
			charset = "UTF-8";
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[65536];
		int count = is.read(buffer);
		while(count > 0){
			baos.write(buffer, 0, count);
			count = is.read(buffer);
		}
		return baos.toString(charset);
	}
	
}
